const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const normalAxios = axios.create();
const mockAxios = axios.create();
import { jsonToTree } from "../utils/tools";
import Config from "./config";
import reqTools from "../utils/reqTools";

// mock 数据
var mock = new MockAdapter(mockAxios);

mock.onGet('/get').reply((config) => {
  return new Promise((resolve, reject) => {
    let res = reqTools.get('/api/sysRole/all')
      .then((res) => {
        resolve([200, res.data]);
      }).catch((err) => {
        reject([500, err]);
      });
  });
});

mock.onGet('/menu').reply((config) => {
  return new Promise((resolve, reject) => {
    reqTools.get('/api/sysMenu').then(res => {
      resolve([200, res.data]);
    }).catch((err) => {
      reject([500, err]);
    });
  });
});

mock.onGet('/roleMenu').reply((config) => {
  return new Promise(function (resolve, reject) {
    reqTools.get('/api/sysRole/roleMenu', { id: config.id })
      .then((res) => {
        resolve([200, res.data]);
      }).catch((err) => {
        reject([500, err]);
      });
  });
});

mock.onGet('/roleManage').reply((config) => {
  return new Promise(function (resolve, reject) {
    reqTools.get('/api/sysRole/roleManage', { id: config.id })
      .then((res) => {
        resolve([200, res.data]);
      }).catch((err) => {
        reject([500, err]);
      });
  });
});

mock.onPost('/roleManage').reply((config) => {
  return new Promise(function (resolve, reject) {
    reqTools.post('/api/sysRole/roleManage',
      config.data
    ).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject([500, err]);
    });
  });
});

mock.onPost('/create').reply((config) => {
  return new Promise(function (resolve, reject) {
    reqTools.post('/api/sysRole',
      config.data
    ).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject([500, err]);
    });
  });
});
mock.onDelete('/del').reply((config) => {
  return new Promise(function (resolve, reject) {
    reqTools.delete('/api/sysRole/' + config.id).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject([500, err]);
    });
  });
});
mock.onPut('/edit').reply((config) => {
  return new Promise(function (resolve, reject) {
    reqTools.put('/api/sysRole',
      config.data,
      { headers: { 'Content-Type': 'application/json' } }
    ).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject([500, err]);
    });
  });
});


export default mockAxios;
